<?PHP

require_once ("config.php");

if (isset($_GET["action"], $_GET["id"], $_GET["x"], $_GET["y"]) && $_GET["action"] == "sensor") {
	$id = (int) $_GET["id"];
	$x = (int) $_GET["x"];
	$y = (int) $_GET["y"];
	
	$db = new SQLite3(CONF_SENSOR_DB_SQLITE);
	if (@$db->exec('UPDATE sensors SET x = '.$x.', y = '.$y.' WHERE id = '.$id)) {
		die('"ok"');
	} else {
		die('"Failed to update DB"');
	}
	
} elseif (isset($_GET["action"], $_GET["last"]) && $_GET["action"] == "pool") {
	
	$last = (double) $_GET["last"] == 0? time()-3600*CONF_HISTORY : $_GET["last"];
	
	$db = new SQLite3(CONF_DATA_SQLITE);
	$results = $db->query('SELECT source, time, num, temp FROM data WHERE time > '.$last);
	$data = array();
	while ($row = $results->fetchArray()) {
		$data[] = '{"id":'.$row["source"].',"time_str":"'.date(CONF_DATE_FORMAT,$row["time"]).'","time":'.$row["time"].',"num":'.$row["num"].',"temp":'.($row["temp"]? $row["temp"]: '""')."}";
	}
	print '{"data": [';
	print (implode(",", $data));
	print "]}";
	
}

